import type { Dayjs } from 'dayjs'
import dayjs from 'dayjs'
import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { getSimpleUsers, ycListSimple } from '@/api/wages/salaryslip'
import { listSimplePosts } from '@/api/system/post'
import { useGlobSetting } from '@/hooks/setting'
import { useMessage } from '@/hooks/web/useMessage'
import { useUserStore } from '@/store/modules/user'

const { createMessage } = useMessage()
const ycListSimpleRes = await ycListSimple()
const userStore = useUserStore()
const colPropsFormSpan = 24

export const columns: BasicColumn[] = [
  {
    title: '员工姓名',
    dataIndex: 'employeeName',
    width: 160,
  },
  {
    title: '部门名称',
    dataIndex: 'deptName',
    width: 160,
  },
  {
    title: '基本工资',
    dataIndex: 'basicSalary',
    width: 160,
  },
  {
    title: '按次工资',
    dataIndex: 'bonus',
    width: 160,
  },
  {
    title: '考核金额',
    dataIndex: 'deductions',
    width: 160,
  },
  {
    title: '工资总额',
    dataIndex: 'total',
    width: 160,
  },
  {
    title: '是否签收',
    dataIndex: 'isAutograph',
    width: 160,
    customRender: ({ text }) => {
      if (text)
        return '已签收'

      else
        return '未签收'
    },
  },
  {
    title: '发放月份',
    dataIndex: 'paymentMonth',
    width: 160,
    customRender: ({ text }) => {
      return useRender.renderDate(text, 'YYYY-MM')
    },
  },
  {
    title: '实际发放日期',
    dataIndex: 'paymentDate',
    width: 160,
    ifShow: false,
    customRender: ({ text }) => {
      return useRender.renderDate(text, 'YYYY-MM-DD')
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
    customRender: ({ text }) => {
      return useRender.renderDate(text)
    },
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: '员工姓名',
    field: 'employeeName',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '部门名称',
    field: 'department',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '基本工资',
    field: 'basicSalary',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '奖金',
    field: 'bonus',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '扣款',
    field: 'deductions',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '发放月份',
    field: 'paymentMonth',
    component: 'RangePicker',
    colProps: { span: 8 },
  },
  {
    label: '实际发放日期',
    field: 'paymentDate',
    component: 'RangePicker',
    colProps: { span: 8 },
  },
  {
    label: '创建时间',
    field: 'createTime',
    component: 'RangePicker',
    colProps: { span: 8 },
  },
]

export const basicInfoSchemas: FormSchema[] = [
  {
    label: '编号',
    field: 'id',
    show: false,
    component: 'Input',
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '员工',
    field: 'employeeId',
    required: true,
    component: 'ApiSelect',
    componentProps: ({ formModel }) => {
      return {
        api: () => getSimpleUsers(''),
        labelField: 'nickname',
        valueField: 'id',
        showSearch: true,
        resultField: 'data',
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase())
        },
        onChange: (e, record) => {
          console.log(e)
          if (record)
            formModel.employeeName = record.label
        },
      }
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '员工姓名',
    field: 'employeeName',
    show: false,
    component: 'Input',
  },
  {
    label: '手机号',
    field: 'phone',
    required: true,
    component: 'Input',
    defaultValue: [],
    rules: [
      {
        pattern: /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/,
        message: '请输入正确的手机号码',
        trigger: 'blur',
      },
    ],
    colProps: { span: colPropsFormSpan },

  },
  {
    label: '部门',
    field: 'deptId',
    required: true,
    component: 'Select',
    componentProps: {
      options: ycListSimpleRes,
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '岗位',
    field: 'post',
    required: true,
    component: 'ApiSelect',
    defaultValue: [],
    componentProps: {
      api: () => listSimplePosts(),
      labelField: 'name',
      valueField: 'id',
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '基本工资',
    field: 'basicSalary',
    required: true,
    component: 'Input',
    componentProps: {
      prefix: '￥',
      style: 'width: 100%',
      stringMode: true,
      precision: 2,
      min: 0,
      max: 999999,
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '按次工资',
    field: 'bonus',
    component: 'Input',
    componentProps: {
      prefix: '￥',
      style: 'width: 100%',
      stringMode: true,
      precision: 2,
      min: 0,
      max: 999999,
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '考核金额',
    field: 'deductions',
    component: 'Input',
    componentProps: {
      prefix: '￥',
      style: 'width: 100%',
      stringMode: true,
      precision: 2,
      min: 0,
      max: 999999,
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '发放月份',
    field: 'paymentMonth',
    required: true,
    component: 'DatePicker',
    componentProps: {
      format: 'YYYY-MM',
      valueFormat: 'x',
      style: 'width:100%',
      picker: 'month',
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '实际发放日期',
    field: 'paymentDate',
    required: true,
    component: 'DatePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'x',
      style: 'width:100%',
    },
    colProps: { span: colPropsFormSpan },
  },
]

export const detailSchema: FormSchema[] = [
  {
    label: '编号',
    field: 'id',
    show: false,
    component: 'Input',
  },
  {
    label: '基本表id',
    field: 'salarySlipId',
    required: false,
    show: false,
    component: 'Input',
  },
  {
    label: '姓名',
    field: 'name',
    required: false,
    show: false,
    component: 'Input',
  },
  {
    label: '乡镇(含企宽)',
    field: 'township',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '城市',
    field: 'city',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '安防',
    field: 'security',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      type: 'number',
      min: 0,
      max: 1000,
    },
  },
  {
    label: '维护',
    field: 'maintain',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '宽带安装',
    field: 'broadbandInstallation',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '单设备',
    field: 'singleDevice',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '一拖一',
    field: 'oneDragOne',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '一拖二',
    field: 'oneDragTwo',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '一拖三',
    field: 'oneDragThree',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '一拖二',
    field: 'oneDragFour',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '室内',
    field: 'indoor',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '室外',
    field: 'outdoor',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '智能组网路由器',
    field: 'networkingRouter',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: 'IPTV',
    field: 'iptv',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '移机',
    field: 'relocation',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '设备入网',
    field: 'networkAccess',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
  {
    label: '按次工资',
    field: 'salaryPerOccurrence',
    required: true,
    component: 'InputNumber',
    componentProps: {
      style: 'width: 100%',
      stringMode: true,
      precision: 0,
      min: 0,
      max: 1000,
    },
  },
]

export const assessmentSchema: FormSchema[] = [
  {
    label: '编号',
    field: 'id',
    show: false,
    component: 'Input',
  },
  {
    label: '基本表id',
    field: 'salarySlipId',
    required: false,
    show: false,
    component: 'Input',
  },
  {
    label: '指标考核',
    field: 'indicator',
    required: true,
    component: 'Input',
  },
  {
    label: '安全考核',
    field: 'safety',
    required: true,
    component: 'Input',
  },
  {
    label: '差旅',
    field: 'training',
    required: true,
    component: 'Input',
  },
  {
    label: '随销考核',
    field: 'reward',
    required: true,
    component: 'Input',
  },
  {
    label: '其他',
    field: 'other',
    required: true,
    component: 'Input',
  },
  {
    label: '自定义字段1',
    field: 'custom1',
    required: false,
    component: 'Input',
  },
  {
    label: '自定义字段2',
    field: 'custom2',
    required: false,
    component: 'Input',
  },
  {
    label: '自定义字段3',
    field: 'custom3',
    required: false,
    component: 'Input',
  },
  {
    label: '自定义字段4',
    field: 'custom4',
    required: false,
    component: 'Input',
  },
  {
    label: '自定义字段5',
    field: 'custom5',
    required: false,
    component: 'Input',
  },
]

export const importExcelSchema: FormSchema[] = [
  {
    label: '部门',
    field: 'deptId',
    required: true,
    component: 'Select',
    defaultValue: 0,
    ifShow: () => userStore.getUserInfo.roles.some(item => item === 'city_admin' || item === 'super_admin' || item === 'wages_admin'),
    componentProps: {
      options: ycListSimpleRes,
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '工资发放月份',
    field: 'month',
    show: true,
    required: true,
    component: 'DatePicker',
    componentProps: () => {
      return {
        format: 'YYYY-MM',
        valueFormat: 'x',
        style: 'width:100%',
        picker: 'month',
        disabledDate: (current: Dayjs) => {
          return current && current > dayjs().endOf('day')
        },
      }
    },
    colProps: { span: colPropsFormSpan },
  },
  {
    label: '是否更新选中月份数据',
    field: 'isUpdate',
    required: true,
    component: 'RadioGroup',
    defaultValue: false,
    componentProps: {
      options: [
        {
          label: '是',
          value: true,
        },
        {
          label: '否',
          value: false,
        },
      ],
    },
  },
  {
    label: '上传excel',
    field: 'importExcel',
    required: true,
    component: 'FileUpload',
    dynamicDisabled: ({ values }) => !values.month && values.isUpdate !== null,
    componentProps: ({ formModel }) => {
      return {
        fileType: 'file',
        maxCount: 1,
        uploadUrl: `${useGlobSetting().apiUrl}/wages/salary-slip/import`,
        data: { month: formModel.month, isUpdate: formModel.isUpdate, deptId: formModel.deptId },
        onChange: (info) => {
          console.log(info)
          if (info !== '')
            createMessage.info('导入结束,请至导入记录管理菜单中查看本次导入结果')
        },
      }
    },
  },

]
